import {Component, OnDestroy, OnInit, ViewEncapsulation} from '@angular/core';
import {ActivatedRoute, Params, Router} from "@angular/router";
import {SaleService} from "@sale/sale.service";

import {ToolsService} from "@shared/service/tools/tools.service";
import {Subject} from "rxjs/Subject";
import {AuthService} from "@shared/service/auth/auth.service";
import {TrainSaleSignFormRespVo} from "@interface/index";


@Component({
  selector: 'seed-add-trial-class',
  templateUrl: './add-trial-class.component.html',
  styleUrls: ['./add-trial-class.component.scss', '../add-trial-campus/add-trial-campus.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AddTrialClassComponent implements OnInit, OnDestroy {

  public orgUserId: string;
  public schoolId: string;
  public studentId: string;
  public saleUserId: string;
  public timesCourseCode: string;
  public allData: Array<TrainSaleSignFormRespVo>;
  public ngUnsubscribe: Subject<void> = new Subject<void>();
  public classInfo = [];

  public courseId: string;

  constructor(
    public route: ActivatedRoute,
    public router: Router,
    public saleService: SaleService,
    public toolsServices: ToolsService,
    private authService: AuthService
  ) {
    this.orgUserId = this.authService.getAuthOrgId();
    this.route.params.subscribe((param: Params) => {
      this.schoolId = param['schoolId'];
      this.courseId = param['courseId'];
      this.studentId = param['studentId'];
      this.saleUserId = param['saleUserId'];
      this.toolsServices.showLoading();
      this.saleService.getCourseList(this.orgUserId, 2).takeUntil(this.ngUnsubscribe).subscribe(res => {
        this.toolsServices.hideLoading();
        if (res.result === '0') {
          this.allData = res.data;
          for (let key in this.allData) {
            if (this.allData[key].trainCourseSimpleVO.id === parseInt(this.courseId, 10)) {
              this.classInfo = [...this.classInfo, ...this.allData[key].trainClassessVOS];
              for (let i = 0; i < this.allData[key].trainTimesCourseRespVOS.length; i++) {
                if (this.allData[key].trainTimesCourseRespVOS[i].timesCourseCode.startsWith('T')) {
                  this.timesCourseCode = this.allData[key].trainTimesCourseRespVOS[i].timesCourseCode;
                }
              }
            }
          };
        }
      });
    });
  }

  /**
   * @description 添加学员到试课,添加成功之后跳转到详情页
   * @author baoww765
   * @date 2018-01-03
   * @param classesId
   * */
  addStudentToTestClass(classesId) {
    const param = {
      orgUserId: this.orgUserId,
      courseId: this.courseId,
      schoolId: this.schoolId,
      classesId: classesId,
      studentId: this.studentId,
      signupFee: 0,
      timesCourseCode: this.timesCourseCode
    };
    this.toolsServices.showLoading();
    this.saleService.addStudentToTestClass(param).takeUntil(this.ngUnsubscribe).subscribe(res => {
      this.toolsServices.hideLoading();
      if (res.result === '0') {
        const that = this;
        this.toolsServices.showToast('添加成功');
        setTimeout(function () {
          that.router.navigate([`/sale/detail/${that.studentId}/${that.saleUserId}`]);
        }, 1500);
      } else {
        this.toolsServices.showToast(res.msg);
      }
    });
  }

  ngOnInit() {
  }

  ngOnDestroy() {
    this.ngUnsubscribe.next();
    this.ngUnsubscribe.complete();
  }
}
